package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/18 下午12:34
 */
@Entity
@Table(name = "term")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update term set term_is_deleted = true where term_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "term_is_deleted = false")
@ApiModel("成绩")
@JsonIgnoreProperties(value={"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Term  extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId",
                      parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "TERM")})
    @Column(name = "term_id")
    @ApiModelProperty(value = "term的id", position = 1)
    private String termId;

    @Column(name = "term_name")
    @ApiModelProperty(value = "学期名称", position = 2)
    private String termName;

    @Column(name = "term_is_forbidden")
    @ApiModelProperty(value = "学期是否被禁用", position = 3)
    private boolean termIsForbidden = false;

    @Column(name = "term_is_deleted")
    @ApiModelProperty(value = "学期是否被删除", position = 4)
    private boolean termIsDeleted = false;

    @OneToMany(mappedBy = "term")
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Score> scores = new ArrayList<>();

    public Term(String termId, String termName, boolean termIsForbidden, boolean termIsDeleted, List<Score> scores) {
        this.termId = termId;
        this.termName = termName;
        this.termIsForbidden = termIsForbidden;
        this.termIsDeleted = termIsDeleted;
        this.scores = scores;
    }

    public Term() {
    }

    public static TermBuilder builder() {
        return new TermBuilder();
    }

    public String getTermId() {
        return this.termId;
    }

    public String getTermName() {
        return this.termName;
    }

    public boolean isTermIsForbidden() {
        return this.termIsForbidden;
    }

    public boolean isTermIsDeleted() {
        return this.termIsDeleted;
    }

    public List<Score> getScores() {
        return this.scores;
    }

    public void setTermId(String termId) {
        this.termId = termId;
    }

    public void setTermName(String termName) {
        this.termName = termName;
    }

    public void setTermIsForbidden(boolean termIsForbidden) {
        this.termIsForbidden = termIsForbidden;
    }

    public void setTermIsDeleted(boolean termIsDeleted) {
        this.termIsDeleted = termIsDeleted;
    }

    public void setScores(List<Score> scores) {
        this.scores = scores;
    }

    public static class TermBuilder {
        private String termId;
        private String termName;
        private boolean termIsForbidden;
        private boolean termIsDeleted;
        private List<Score> scores;

        TermBuilder() {
        }

        public TermBuilder termId(String termId) {
            this.termId = termId;
            return this;
        }

        public TermBuilder termName(String termName) {
            this.termName = termName;
            return this;
        }

        public TermBuilder termIsForbidden(boolean termIsForbidden) {
            this.termIsForbidden = termIsForbidden;
            return this;
        }

        public TermBuilder termIsDeleted(boolean termIsDeleted) {
            this.termIsDeleted = termIsDeleted;
            return this;
        }

        public TermBuilder scores(List<Score> scores) {
            this.scores = scores;
            return this;
        }

        public Term build() {
            return new Term(termId, termName, termIsForbidden, termIsDeleted, scores);
        }

        public String toString() {
            return "Term.TermBuilder(termId=" + this.termId + ", termName=" + this.termName + ", termIsForbidden=" + this.termIsForbidden + ", termIsDeleted=" + this.termIsDeleted + ", scores=" + this.scores + ")";
        }
    }
}
